﻿namespace CSharpPro._1027;

using System;
using System.Collections.Generic;

/// <summary>
/// 儿童类，用于表示儿童的基本信息和行为特征
/// </summary>
public class Child
{
    // 私有字段
    private string _name;               // 姓名
    private int _age;                   // 年龄（儿童通常指0-12岁）
    private string _gender;             // 性别
    private List<string> _toys;         // 拥有的玩具
    private string _favoriteFood;       // 最喜欢的食物
    private bool _isAsleep;             // 是否在睡觉

    // 公共属性（带数据验证）
    public string Name
    {
        get => _name;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("姓名不能为空或空白", nameof(value));
            _name = value;
        }
    }

    public int Age
    {
        get => _age;
        set
        {
            if (value < 0 || value > 12)
                throw new ArgumentOutOfRangeException(nameof(value), "儿童年龄应在0-12岁之间");
            _age = value;
        }
    }

    public string Gender
    {
        get => _gender;
        set
        {
            string lowerValue = value?.Trim().ToLower();
            if (lowerValue != "男" && lowerValue != "女" && !string.IsNullOrWhiteSpace(lowerValue))
                throw new ArgumentException("性别只能是'男'或'女'", nameof(value));
            _gender = lowerValue ?? string.Empty;
        }
    }

    public string FavoriteFood
    {
        get => _favoriteFood;
        set => _favoriteFood = value ?? string.Empty;
    }

    public bool IsAsleep
    {
        get => _isAsleep;
        private set => _isAsleep = value; // 只能通过方法修改睡眠状态
    }

    // 只读属性：玩具数量
    public int ToyCount => _toys.Count;

    // 构造函数
    public Child(string name, int age)
    {
        Name = name;
        Age = age;
        _gender = string.Empty;
        _toys = new List<string>();
        _favoriteFood = string.Empty;
        _isAsleep = false;
    }

    public Child(string name, int age, string gender) : this(name, age)
    {
        Gender = gender;
    }

    // 方法：添加玩具
    public void AddToy(string toyName)
    {
        if (string.IsNullOrWhiteSpace(toyName))
            throw new ArgumentException("玩具名称不能为空或空白", nameof(toyName));
        
        if (!_toys.Contains(toyName))
        {
            _toys.Add(toyName);
            Console.WriteLine($"{Name}得到了新玩具：{toyName}");
        }
        else
        {
            Console.WriteLine($"{Name}已经有这个玩具了：{toyName}");
        }
    }

    // 方法：玩玩具
    public void PlayWithToy(string toyName)
    {
        if (IsAsleep)
        {
            Console.WriteLine($"{Name}正在睡觉，不能玩玩具哦~");
            return;
        }

        if (_toys.Contains(toyName))
        {
            Console.WriteLine($"{Name}正在开心地玩{toyName}");
        }
        else
        {
            Console.WriteLine($"{Name}没有{toyName}这个玩具");
        }
    }

    // 方法：显示所有玩具
    public void ShowToys()
    {
        if (_toys.Count == 0)
        {
            Console.WriteLine($"{Name}还没有玩具");
            return;
        }

        Console.WriteLine($"{Name}的玩具列表：");
        foreach (var toy in _toys)
        {
            Console.WriteLine($"- {toy}");
        }
    }

    // 方法：睡觉
    public void Sleep()
    {
        if (IsAsleep)
        {
            Console.WriteLine($"{Name}已经睡着了zzz");
        }
        else
        {
            IsAsleep = true;
            Console.WriteLine($"{Name}乖乖睡觉了zzz");
        }
    }

    // 方法：醒来
    public void WakeUp()
    {
        if (!IsAsleep)
        {
            Console.WriteLine($"{Name}本来就醒着呀");
        }
        else
        {
            IsAsleep = false;
            Console.WriteLine($"{Name}睡醒啦，精神真好！");
        }
    }

    // 方法：吃东西
    public void Eat(string food)
    {
        if (IsAsleep)
        {
            Console.WriteLine($"{Name}正在睡觉，不能吃东西哦");
            return;
        }

        string reaction = string.Equals(food, FavoriteFood, StringComparison.OrdinalIgnoreCase)
            ? "太好吃啦！这是我最喜欢的食物~"
            : "真美味！";
        
        Console.WriteLine($"{Name}在吃{food}，{reaction}");
    }

    // 重写ToString方法
    public override string ToString()
    {
        string genderInfo = string.IsNullOrWhiteSpace(Gender) ? "" : $"{Gender}孩";
        return $"{Name}（{Age}岁{genderInfo}）";
    }
}
        
        
    
